import {AutoLogin,LogOut,reqShopgoods,reqShopinfo,reqShopingrating} from '../api/index'
import {STOREUSER,RESETUSER,RECEIVESHOPGOOD,RECEIVESHOPINFO,RECEIVESHOPRATING} from './action-type'
export default {
  //存储用户信息的同步action，登录时使用
  store_user ({commit},userInfo) {
    commit(STOREUSER,userInfo)
  },
  //获取用户信息的异步action，实现自动登录
  async GetUser ({commit}) {
    let result = await AutoLogin()
    if(result.code === 0){
      commit(STOREUSER,result.data)
    }
  },
  //用户登出
  async LogOut ({commit}) {
    let result = await LogOut()
    if(result.code === 0){
      commit(RESETUSER)
    }
  },
  //获取食物信息
  async GetGoods ({commit},callback) {
    let result = await reqShopgoods()
    if(result.code === 0){
      commit(RECEIVESHOPGOOD,result.data)
      //通知组件数据获取成功，设置滚动
      callback && callback()
    }
  },
  //获取商家信息
  async GetShopInfo ({commit}) {
    let result = await reqShopinfo()
    if(result.code === 0){
      commit(RECEIVESHOPINFO,result.data)
    }
  },
  //获取评价信息
  async GetRating ({commit}) {
    let result = await reqShopingrating()
    if(result.code === 0){
      commit(RECEIVESHOPRATING,result.data)
    }
  }
  
}